<template>
  <div class="login">
    <div style="height: 160px"></div>
    <el-form
      ref="loginForm"
      :model="loginForm"
      :rules="loginRules"
      class="login-form">
      <!--cms管理系统-->
      <div class="title-container">
        后台管理系统
      </div>
      <!--账号-->
      <el-form-item prop="userName">
        <span class="svg-container">
          <svg-icon icon-class="user" />
        </span>
        <el-input
          v-model="loginForm.userName"
          size="medium"
          placeholder="请输入账号">
        </el-input>
      </el-form-item>
      <!--密码-->
      <el-form-item prop="passWord">
        <span class="svg-container">
          <svg-icon icon-class="password" />
        </span>
        <el-input
          ref="passWord"
          v-model="loginForm.passWord"
          size="medium"
          :type="passwordType"
          placeholder="请输入密码"
          @keyup.enter.native="login">
        </el-input>
        <span class="show-pwd" @click="showPwd">
          <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
        </span>
      </el-form-item>
      <!--登录-->
      <el-button
        v-preventReClick
        :disabled="loading"
        :loading="loading"
        type="primary"
        size="default"
        class="full-width"
        @click="login">
        登录
      </el-button>
      <!--显示登陆账号密码-->
      <div class="mt-15">账号：admin 密码：随意</div>
      <div class="mt-10">账号：ordinary 密码：随意</div>
    </el-form>
  </div>
</template>

<script>
import md5 from 'js-md5'
import { deepClone } from '@/utils'
import store from '../../store'

export default {
  name: 'Login',
  data() {
    return {
      loginForm: {
        userName: 'admin',
        passWord: 'admin'
      },
      loading: false,
      passwordType: 'password',
      loginRules: {},
      redirect: ''
    }
  },
  watch: {
    $route: {
      handler: function(route) {
        const query = route.query
        if (query) {
          this.redirect = query.redirect
        }
      },
      immediate: true
    }
  },
  mounted() {
  },
  methods: {
    // 是否显示密码
    showPwd() {
      this.passwordType = this.passwordType === 'password' ? 'text' : 'password'
      this.$nextTick(() => {
        this.$refs.passWord.focus()
      })
    },
    // 登录
    login() {
      this.$refs.loginForm.validate(async(valid) => {
        if (valid) {
          this.loading = true
          let params = deepClone(this.loginForm)
          params.passWord = md5(this.loginForm.passWord) // md5加密
          store.dispatch('user/login', params)
            .then(() => {
              this.$router.push({ path: this.redirect || '/' })
              this.loading = false
            })
            .catch(() => {
              this.loading = false
            })
        } else {
          return false
        }
      })
    }
  }
}
</script>


<style lang="scss">
$cursor: #fff;

/* 重置 element-ui 的 css */
.login {
  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;

    input {
      background: transparent;
      border: 0;
      -webkit-appearance: none;
      border-radius: 0;
      padding: 12px 5px 12px 15px;
      height: 47px;
    }
  }

  .el-form-item {
    border: 1px solid rgba(255, 255, 255, 0.1);
    background: rgba(0, 0, 0, 0.1);
    border-radius: 5px;
    color: #454545;
  }
}
</style>
<style scoped lang="scss">
.login {
  height: 100vh;
  width: 100%;
  background-image: url('../../assets/background.png');
  background-position: center 0;
  background-repeat: no-repeat;
  background-attachment: fixed;
  background-size: cover;
  -webkit-background-size: cover;
  -o-background-size: cover;
  zoom: 1;

  .login-form {
    position: relative;
    width: 520px;
    max-width: 100%;
    padding: 35px;
    margin: 0 auto;
    overflow: hidden;
    border-radius: 10px;
    background-color: #fff;
  }

  .title-container {
    font-size: 26px;
    margin: 0 auto 40px auto;
    text-align: center;
    font-weight: bold;
  }

  .svg-container {
    padding: 6px 5px 6px 15px;
    color: #43484B;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: #43484B;
    cursor: pointer;
    user-select: none;
  }
}
</style>
